package com.ruoyi.system.api.factory;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件服务降级处理
 *
 * @author ruoyi
 */
@Component
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);

    @Override
    public RemoteFileService create(Throwable throwable) {
        log.error("文件服务调用失败:{}", throwable.getMessage());
        return new RemoteFileService() {
            @Override
            public R<SysFile> upload(MultipartFile file) {
                return R.fail("上传文件失败:" + throwable.getMessage());
            }

            @Override
            public R update(SysFile sysFile) {
                return R.fail("修改文件失败:" + throwable.getMessage());
            }

            /**
             * 获取文件
             *
             * @param id
             * @return
             */
            @Override
            public R<SysFile> getOne(Long id) {
                return R.fail("获取文件失败:" + throwable.getMessage());
            }

            /**
             * 通过关联id获取附件
             *
             * @param linkId
             * @return
             */
            @Override
            public R<List<SysFile>> getList(String linkId) {
                return R.fail("通过关联id获取附件失败:" + throwable.getMessage());
            }

            /**
             * 通过linkId删除文件
             *
             * @param linkId 关联id
             * @return
             */
            @Override
            public R<SysFile> deleteLinkFile(String linkId) {
                return R.fail("通过linkId删除文件失败:" + throwable.getMessage());
            }
        };
    }
}
